package com.nbm.oa.client;
/**
 * 点最小化托盘
 */
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class sp extends JFrame
{

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	/**
	 * 托盘图标
	 */
	private Image icon;

	private TrayIcon trayIcon;

	/**
	 * 系统托盘
	 */
	private SystemTray systemTray;

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize()
	{

		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");

	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				sp thisClass = new sp();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public sp()
	{

		super("系统托盘标");
		initialize();
		systemTray = SystemTray.getSystemTray();// 获得系统托盘的实例
		setSize(200, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);

		try
		{
			icon = new ImageIcon("0.gif").getImage();
			trayIcon = new TrayIcon(icon, "托盘图标");
			systemTray.add(trayIcon);// 设置托盘的图标，0.gif与该类文件同一目录
		} catch (AWTException e2)
		{
			e2.printStackTrace();
		}

		this.addWindowListener(new WindowAdapter()
		{
			public void windowIconified(WindowEvent e)
			{
				dispose();// 窗口最小化时dispose该窗口
			}
		});

		trayIcon.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{// 双击托盘窗口再现
					setExtendedState(Frame.NORMAL);// 状态
					setVisible(true);
				}
			}
		});
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	/*
	 * private void initialize() { this.setSize(300, 200);
	 * this.setContentPane(getJContentPane()); this.setTitle("JFrame"); }
	 */

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

}
